//index.js
//获取应用实例
var app=getApp()
const { formatTime3 } = require('../../utils/util.js');
var util = require('../../utils/util.js');
const db = wx.cloud.database();
const _ = db.command;
Page({
  data:{
    experience:"10",
    xingren:"15",
    recite:[],
    test:[],
    realTest:[],
    star:[],
    msg:[],
    days:"",
    src:"cloud://test-hf5g2.7465-test-hf5g2-1304459590/search.jpg",
    str:"",
    signDay:"0",
    reciteDay:"0",
    exist:false,
    openid:"",
    url:"",
    nickName:"",
    aggister:[
      '../img/img/ztzs.png','../img/img/zyzd.png','../img/img/spzzd.png'
    ],
    swiper_img:[
      '../img/img/banner-2.png',
      '../img/img/banner-2.png',
      '../img/img/banner-2.png',
      '../img/img/banner-2.png',
    ]
  },
  onLoad(){
    let that=this
    wx.showLoading({
      title: '加载中',
    })
    if (wx.getStorageSync('openid')) {
      db.collection("userList").where({
        openid:wx.getStorageSync('openid')   //进行筛选
      }).get({
        success:res=>{
            console.log(res)
            console.log(res.data[0].days)
            that.setData({
              exist:true,
              openid:res.data[0].openid,
              nickName:res.data[0].nickName,
              url:res.data[0].avatarUrl,
              experience:res.data[0].experience,
              xingren:res.data[0].xingren,
              days:res.data[0].days,
              signDay:res.data[0].signDay,
              reciteDay:res.data[0].reciteDay,
              recite:res.data[0].recite,
              test:res.data[0].test,
              realTest:res.data[0].realTest,
              star:res.data[0].star,
              msg:res.data[0].msg,
            })
          }
      })
    }
    else{
      wx.cloud.callFunction({
      name:"getOpenId",
      complete: res => {
        console.log(res);
        db.collection("userList").where({
          openid:res.result.openid   //进行筛选
        }).get({
          success:res=>{
            console.log(res.data.length)
            if(res.data.length==0){
              that.setData({
                exist:false
              })
            }
            else
            {
              that.setData({
                exist:true,
                openid:res.data[0].openid,
                nickName:res.data[0].nickName,
                url:res.data[0].avatarUrl,
                experience:res.data[0].experience,
                xingren:res.data[0].xingren,
                recite:res.data[0].recite,
                days:res.data[0].days,
                signDay:res.data[0].signDay,
                reciteDay:res.data[0].reciteDay,
                test:res.data[0].test,
                realTest:res.data[0].realTest,
                star:res.data[0].star,
                msg:res.data[0].msg,
              })
                wx.setStorageSync('nickName', that.data.nickName)
                wx.setStorageSync('url', that.data.url)
                wx.setStorageSync('openid', that.data.openid)
            }
          }
        })
        if (that.data.exist==false) {
          console.log(res.result.openid)
          that.setData({
            
          openid:res.result.openid
          })
        }
      }
    })
  }
  wx.hideLoading({
    success: (res) => {},
  })
  },
  onShow()
  {
    this.setData({
      str:""
    });
    wx.setStorageSync('searchObject', []);
    var that=this
    let now=formatTime3(new Date())
    let days=that.data.days
    console.log(now)
    console.log(days)
    if(days==null)
    {
      app.globalData.changeDays=true;
      wx.cloud.callFunction({
        name:'updata',
        data:{
          openid:wx.getStorageSync('openid'),
          action:'userList_update',
          days:now,
          signDay:Number(that.data.signDay)+1,
          experience:Number(that.data.experience)+5,
          xingren:Number(that.data.xingren)+5
        }
      })
          wx.showModal({
            content:'签到成功 '+' 经验值加5 '+' 杏仁加5'
          })
          console.log("签到成功")
          that.setData({
            days:now
          })
    }
    else
    {
    let Now=now.split('-');
    let Days=days.split('-');
    console.log(Now)
    console.log(Days)
      if(Now[0]-Days[0]==0)//年份相同
      {
        if(Now[1]-Days[1]==0)//月份相同
        {
          if(Now[2]-Days[2]==1)
          {
            wx.cloud.callFunction({
              name:'updata',
              data:{
                openid:wx.getStorageSync('openid'),
                action:'userList_update',
                days:now,
                signDay:Number(that.data.signDay)+1,
                experience:Number(that.data.experience)+5,
                xingren:Number(that.data.xingren)+5 
              }
            })
            app.globalData.changeDays=true;
            wx.showModal({
            title:'签到日历',
            content:'连续签到'+that.data.signDay+'天'
          })
          console.log("连续签到成功")
          that.setData({
            days:now
          })
          }
          else if(Now[2]-Days[2]==0)
          {
            return;
          }
          else
          {
            app.globalData.changeDays=true;
            wx.cloud.callFunction({
              name:'updata',
              data:{
                openid:wx.getStorageSync('openid'),
                action:'userList_update',
                days:now,
                signDay:0,
                experience:Number(that.data.experience)+5,
                xingren:Number(that.data.xingren)+5
              }
            })
            wx.showModal({
            content:'签到成功'
            })
           console.log("签到成功")
           that.setData({
            days:now
          })
          }
        }
        else if(Now[1]-Days[1]==1||Now[1]-Days[1]==-11)
        {
          if(Now[1]==1||Now[1]==2||Now[1]==4||Now[1]==6||Now[1]==9||Now[1]==11)
          {
            if(Now[2]-Days[2]==-30)
            {
              app.globalData.changeDays=true;
              wx.cloud.callFunction({
                name:'updata',
                data:{
                  openid:wx.getStorageSync('openid'),
                  action:'userList_update',
                  days:now,
                  signDay:Number(that.data.signDay)+1,
                  experience:Number(that.data.experience)+5,
                  xingren:Number(that.data.xingren)+5
                }
              })
            wx.showModal({
              title:'签到日历',
              content:'连续签到'+that.data.signDay+'天'
            })
            console.log("连续签到成功")
            that.setData({
              days:now
            })
            }
            else
            {
              app.globalData.changeDays=true;
              wx.cloud.callFunction({
                name:'updata',
                data:{
                  openid:wx.getStorageSync('openid'),
                  action:'userList_update',
                  days:now,
                  signDay:0,
                  experience:Number(that.data.experience)+5,
                  xingren:Number(that.data.xingren)+5
                }
              })
            wx.showModal({
              content:'签到成功'
            })
            console.log("签到成功")
            that.setData({
              days:now
            })
            }
          }
          else if(Now[1]==5||Now[1]==7||Now[1]==8||Now[1]==10||Now[1]==12)
         {
            if(Now[2]-Days[2]==-29)
            {
              app.globalData.changeDays=true;
              wx.cloud.callFunction({
                name:'updata',
                data:{
                  openid:wx.getStorageSync('openid'),
                  action:'userList_update',
                  days:now,
                  signDay:Number(that.data.signDay)+1,
                  experience:Number(that.data.experience)+5,
                  xingren:Number(that.data.xingren)+5
                }
              })
            wx.showModal({
              title:'签到日历',
              content:'连续签到'+that.data.signDay+'天'
            })
            console.log("连续签到成功")
            that.setData({
              days:now
            })
            }
           else
          {
            app.globalData.changeDays=true;
            wx.cloud.callFunction({
              name:'updata',
              data:{
                openid:wx.getStorageSync('openid'),
                action:'userList_update',
                days:now,
                signDay:0,
                experience:Number(that.data.experience)+5,
                xingren:Number(that.data.xingren)+5
              }
            })
          wx.showModal({
            content:'签到成功'
          })
          console.log("签到成功")
          that.setData({
            days:now
          })
          }
        }
        else if(Now[1]==3)
        {
          if(Now[2]-Days[2]==-28||Now[2]-Days[2]==-27)
          {
            app.globalData.changeDays=true;
            wx.cloud.callFunction({
              name:'updata',
              data:{
                openid:wx.getStorageSync('openid'),
                action:'userList_update',
                days:now,
                signDay:Number(that.data.signDay)+1,
                experience:Number(that.data.experience)+5,
                xingren:Number(that.data.xingren)+5
              }
            })
            wx.showModal({
            title:'签到日历',
            content:'连续签到'+that.data.signDay+'天'
          })
          console.log("连续签到成功")
          that.setData({
            days:now
          })
          }
          else
          {
            app.globalData.changeDays=true;
            wx.cloud.callFunction({
              name:'updata',
              data:{
                openid:wx.getStorageSync('openid'),
                action:'userList_update',
                days:now,
                signDay:0,
                experience:Number(that.data.experience)+5,
                xingren:Number(that.data.xingren)+5
              }
            })
          wx.showModal({
            content:'签到成功'
          })
          console.log("签到成功")
          that.setData({
            days:now
          })
          }
        }
      }
      }
    }
  },
  getData()
  {
    wx.cloud.database().collection("questioList").get({
      success(res){
          var i=0;
          console.log("成功获取数据",res.data[i].B);
      },
      fail(res){
        console.log("获取数据失败",res);
      }
    })
  },
  toCourse()
  {
    wx.navigateTo({
      url: '../course/course'
    })
  },
  to_test:function()
  {
    wx.navigateTo({
      url: '../home/home'
    })
  
  },
  toRoot:function()
  {
    wx.navigateTo({
      url: '../dictRoot/dictRoot',
    })
  },
  to_recite:function()
  {
    console.log(this.data.recite,"1")
    wx.navigateTo({
      // ?recite='+this.data.recite
      url: '../reciteRoot/recitrRoot',
    })
  },
  toWordRoot(){
    wx.navigateTo({
      url: '../wordDictRoot/wordDictRoot'
    })
  },
  getStr(e){
    console.log(e.detail.value)
    this.data.str=e.detail.value
  },
  search(){
    let key=this.data.str
    db.collection('Dictionary').where(
      _.or([{
      name: db.RegExp({
        regexp: '.*' + key,
        options: 'i',
      })
    },
    // {
    //   address: db.RegExp({
    //     regexp: '.*' + key,
    //     options: 'i',
    //   })
    // }
  ])
  ).get({
    success: res => {
      console.log(res)
      if(res.data.length>0){
        wx.setStorageSync('searchObject', res.data)
        wx.navigateTo({
          url: '../dictRoot2/dictRoot2',
        })
      }else{
        wx.showToast({
          title: '暂无数据！',
          icon: "error",
          mask: true
        })
        console.log(JSON.parse(wx.getStorageSync('addList')))
      }
    },
    fail: err => {
      console.log(err)
    }
  })
  }
})
